﻿using MyECommerceProject.Interfaces;
using MyECommerceProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyECommerceProject.Services
{
    public class ProductService : IProductService
    {
        private readonly IProductRepository _productRepository;

        public ProductService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        //获取所有
        public async Task<IEnumerable<Product>> GetProductsAsync()
        {
            return await _productRepository.GetProductsAsync();
        }
        //通过ID获取
        public async Task<Product> GetProductByIdAsync(int productId)
        {
            var result = await _productRepository.GetProductByIdAsync(productId);
            return result;
        }

        //添加

        public async Task AddProductAsync(Product product)
        {
            await _productRepository.AddProductAsync(product);
        }
        //更新

        public async Task UpdateProductAsync(Product product)
        {
            await _productRepository.UpdateProductAsync(product);
        }
        //删除

        public async Task DeleteProductAsync(int productId)
        {
            await _productRepository.DeleteProductAsync(productId);
        }

        //排序和分页查询
        public async Task<IEnumerable<Product>> GetProductsSortedByAsync(Func<Product, object> keySelector, bool ascending = true)
        {
            var products = await _productRepository.GetProductsAsync();
            return ascending ? products.OrderBy(keySelector) : products.OrderByDescending(keySelector);
        }

        public async Task<IEnumerable<Product>> GetProductsFilteredByAsync(Func<Product, bool> predicate)
        {
            var products = await _productRepository.GetProductsAsync();
            return products.Where(predicate);
        }

        public async Task<IEnumerable<Product>> GetProductsByOptionAsync(ProductSearchOption option)
        {

            var result = await _productRepository.GetProductsAsyncByOptionAsync(option);
            /***
             * 
             * 
             * 一些复杂的业务逻辑
             */

            return result;
        }




    }
}
